﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GazziWpf.Application.Core.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;

namespace GazziWpf.Application.Core.ViewModels
{
   public class ViewModelBase<TEntity>: BindableBase
    {
        #region 屬性
        private TEntity _entity;
        public TEntity Entity
        {
            get { return _entity; }
            set { SetProperty(ref _entity, value); }
        }

        private ObservableCollection<TEntity> _entity_List;
        public ObservableCollection<TEntity> Entity_List
        {
            get { return _entity_List; }
            set { SetProperty(ref _entity_List, value); }
        }





        private string _filterText;
        /// <summary>
        /// 搜索字段
        /// </summary>
        public string filterText
        {
            get { return _filterText; }
            set { SetProperty(ref _filterText, value); }
        }


        private int _totalCount;
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount; }
            set { SetProperty(ref _totalCount, value); }
        }


        /// <summary>
        /// 总页数
        /// </summary>
        public int pagecount { get; set; }



        private int _pagesize = 10;
        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize
        {
            get { return _pagesize; }
            set
            {
                if (value > 0 && value < 100)
                {
                    SetProperty(ref _pagesize, value);

                }
            }
        }

        private int _pageno = 1;
        /// <summary>
        /// 当前页号
        /// </summary>
        public int PageNo
        {
            get => _pageno;
            set
            {
                if (value > 0)
                {
                    _pageno = value;
                    this.NextPageCommand.RaiseCanExecuteChanged();
                    this.LastPageCommand.RaiseCanExecuteChanged();
                }
            }
        }

        protected IEventAggregator _ea { set; get; }

        #endregion

        public ViewModelBase (IEventAggregator eventAggregator)
        {
            this._ea = eventAggregator;
            this._ea.GetEvent<FormEvent<TEntity>>().Subscribe(OnWindowActive);
            GetCurrentPageList();
        }


        #region 命令

        private DelegateCommand _cancleCommand;
        public DelegateCommand CancleCommand =>
            _cancleCommand ?? (_cancleCommand = new DelegateCommand(ExecuteCancleCommand, CanExecuteCancleCommand));



        private DelegateCommand _comfirmCommand;
        public DelegateCommand ComfirmCommand =>
            _comfirmCommand ?? (_comfirmCommand = new DelegateCommand(ExecuteComfirmCommand, CanExecuteComfirmCommand));


        private DelegateCommand _ClearCommand;
        public DelegateCommand ClearCommand =>
            _ClearCommand ?? (_ClearCommand = new DelegateCommand(ExecuteClearCommand, CanExecuteClearCommand));




        private DelegateCommand _addCommand;

        public DelegateCommand AddCommand =>
            _addCommand ?? (_addCommand = new DelegateCommand(ExecuteCommandName, CanExecuteCommandName));


        private DelegateCommand _editCommand;

        public DelegateCommand EditCommand =>
            _editCommand ?? (_editCommand = new DelegateCommand(ExecuteEditCommand, CanExecuteEditCommand));

        private DelegateCommand _checkListItemCommand;
        public DelegateCommand CheckListItemCommand =>
            _checkListItemCommand ?? (_checkListItemCommand = new DelegateCommand(ExecuteCheckListItemCommand, CanExecuteCheckListItemCommand));


        private DelegateCommand _nextpage;
        public DelegateCommand NextPageCommand =>
            _nextpage ?? (_nextpage = new DelegateCommand(ExecuteNextPageCommand, CanExecuteNextPageCommand));


        private DelegateCommand _lastpage;
        public DelegateCommand LastPageCommand =>
            _lastpage ?? (_lastpage = new DelegateCommand(ExecuteLastPageCommand, CanExecuteLastPageCommand));


        private DelegateCommand _searchcmd;
        public DelegateCommand SearchCommand =>
            _searchcmd ?? (_searchcmd = new DelegateCommand(ExecuteSearchCommand, CanExecuteSearchCommand));



        #endregion

        #region 方法

        public virtual void OnWindowActive(TEntity entity)
        {
            this.Entity = entity;
        }



        public virtual void GetCurrentPageList()
        {
            LastPageCommand.RaiseCanExecuteChanged();
            NextPageCommand.RaiseCanExecuteChanged();

        }
        public virtual void whenOpenform<T>(T t)
        {

        }
        public virtual void ExecuteCancleCommand()
        {

        }

        bool CanExecuteCancleCommand()
        {
            return true;
        }
        public virtual void ExecuteComfirmCommand()
        {

        }
        public virtual void ExecuteCheckListItemCommand()
        {

        }

        bool CanExecuteCheckListItemCommand()
        {
            return true;
        }
        public virtual bool CanExecuteComfirmCommand()
        {
            return true;
        }


        public virtual bool CanExecuteCommandName()
        {
            return true;
        }
        /// <summary>
        /// 添加
        /// </summary>
        public virtual void ExecuteCommandName()
        {
        }
        public virtual bool CanExecuteEditCommand()
        {
            return true;
        }

        public virtual void ExecuteEditCommand()
        {
        }

        public virtual void ExecuteClearCommand()
        {

        }

        public virtual bool CanExecuteClearCommand()
        {
            return true;
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public virtual void ExecuteNextPageCommand()
        {
            if (this.PageNo < pagecount)
            {
                this.PageNo++;
                GetCurrentPageList();
            }
            else
            {
                UMessageBox.Show("提示", "已经是最后一页了", System.Windows.MessageBoxButton.OK);
            }
           
        }

        public virtual bool CanExecuteNextPageCommand()
        {

            return this.PageNo < pagecount;
        }
        /// <summary>
        /// 上一页
        /// </summary>
        public virtual void ExecuteLastPageCommand()
        {
            if (this.PageNo > 1)
            {
                this.PageNo--;
                GetCurrentPageList();
            }
            else
            {
                UMessageBox.Show("提示", "已经是第一页了", System.Windows.MessageBoxButton.OK);
            }

            this.LastPageCommand.RaiseCanExecuteChanged();
        }

        public virtual bool CanExecuteLastPageCommand()
        {
            return this.PageNo > 1;
        }
        /// <summary>
        /// 搜索
        /// </summary>
        public virtual void ExecuteSearchCommand()
        {
            this.PageNo = 1;
            GetCurrentPageList();
        }

        bool CanExecuteSearchCommand()
        {
            return true;
        }

        #endregion
    }
}
